package net.dreamlu.common;

import java.util.List;

import net.dreamlu.qa.interceptor.SearchInterceptor;
import net.dreamlu.qa.model.Question;
import net.dreamlu.qa.model.Tags;
import net.dreamlu.qa.model.User;
import net.dreamlu.util.PageUtil;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.StringKit;
import com.jfinal.plugin.ehcache.CacheInterceptor;

/**
 * CommonController
 */
public class CommonController extends Controller {
		
	// 首页
	@Before(CacheInterceptor.class)
	public void index() {
		int pageNum = getPara("page") == null ? 1 : getParaToInt("page");
		PageUtil page = new PageUtil(pageNum, Question.dao.getCount(), getPara());
		List<Question> questionList = Question.dao.getPage(pageNum, page.getPageSize()).getList();
		setAttr("questionList", questionList);
		setAttr("tab", "index");
		setAttr("page", page);
		render("/solution/index.jsp");
	}
	
	// 首页热点
	public void hottest() {
		int pageNum = getPara("page") == null ? 1 : getParaToInt("page");
		Integer hotCount = Question.dao.getHotCount();
		PageUtil page = new PageUtil(pageNum, hotCount, getPara());
		List<Question> questionList = Question.dao.getHotPage(pageNum, page.getPageSize()).getList();
		setAttr("questionList", questionList);
		setAttr("tab", "hot");
		setAttr("page", page);
		render("/index.jsp");
	}
	
	// 首页 无回答
	public void unanswered() {
		int pageNum = getPara("page") == null ? 1 : getParaToInt("page");
		Integer coldCount = Question.dao.getColdCount();
		PageUtil page = new PageUtil(pageNum, coldCount, getPara());
		List<Question> questionList = Question.dao.getColdPage(pageNum, page.getPageSize()).getList();
		setAttr("questionList", questionList);
		setAttr("tab", "cold");
		setAttr("page", page);
		render("/index.jsp");
	}
	
	// 搜索 拦截 q 为空
	@Before(SearchInterceptor.class)
	public void search (){
		String tab = getPara("tab");
		String q = getPara("q");
		int pageNum = getPara("page") == null ? 1 : getParaToInt("page");
		PageUtil page = null;
		if(StringKit.notBlank(tab) && tab.equals("tag")){
			page = new PageUtil(pageNum, Tags.dao.getLikeCount(q), getPara());
			List<Tags> tagsList = Tags.dao.getLinkPage(pageNum, page.getPageSize(), q).getList();
			setAttr("tagsList", tagsList);
			setAttr("page", page);
			setAttr("tab", tab);
			setAttr("q", q);
			render("/solution/search_tags.jsp");
		}else if(StringKit.notBlank(tab) && tab.equals("user")){
			page = new PageUtil(pageNum, User.dao.getLikeCount(q), getPara());
			List<User> userList = User.dao.getLikePage(pageNum, page.getPageSize(), q).getList();
			setAttr("userList", userList);
			setAttr("page", page);
			setAttr("tab", tab);
			setAttr("q", q);
			render("/solution/search_user.jsp");
		}else{
			page = new PageUtil(pageNum, Question.dao.getLikeCount(q), getPara());
			List<Question> questionList = Question.dao.getLikePage(pageNum, page.getPageSize(), q).getList();
			setAttr("questionList", questionList);
			setAttr("page", page);
			setAttr("tab", tab);
			setAttr("q", q);
			render("/solution/search_question.jsp");
		}
	}
	
	
	// 提问
	public void ask() {
		User user = getSessionAttr("user");
		setAttr("ref", getPara("ref")); 
		if(user != null){
			// 设置Toke防止重复提交
			createToken("questionToken", 30*60);
			render("/solution/ask.jsp");
		}else {
			render("/account/login.jsp");
		}
		
	}
	
	public void list() {
		List<Question> quesList = Question.dao.find("select q.*,a.* from question q, answer a where q.del_status = 0 and a.del_status =0 and q.id = a.quest_id order by q.id desc");  
		
		for (Question question : quesList) {
			System.out.println(question.get("jack_num"));
		}
		renderText("");
	}
}
